#ifndef __INK_INET_H__
#define __INK_INET_H__

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>  

class IpEntry
{
public:
	IpEntry();
	void init(sockaddr *addr);
	void init(in_addr_t ip, int port);
	void init(in6_addr ip, int port);
	void init(const IpEntry & entry);
	void init(int family, const char *ip, int port);
	IpEntry &operator=(const IpEntry &entry);
	~IpEntry();

	static bool is_ipv4(const sockaddr *addr);
	static bool is_ipv6(const sockaddr *addr);

	bool is_valid() const;

	int get_port_h();	//get network bytes order port
	int get_port_n();	//get host bytes order port
	bool get_ip_string(char *ip, int len);
	int get_family();

	sockaddr *get_addr();

private:
	IpEntry &assign();

	

	int _family;	//protocol family
	int _port; 		//network bytes order

	union{
		in_addr_t ip4;
		in6_addr ip6;
	}_ip;

	sockaddr *_addr;
};

static inline in_addr_t
ipv4_addr_cast(const sockaddr *addr)
{
	return IpEntry::is_ipv4(addr) ? ((sockaddr_in *)addr)->sin_addr.s_addr : 0;
}

static inline in6_addr
ipv6_addr_cast(const sockaddr *addr)
{
	return ((sockaddr_in6 *)addr)->sin6_addr;
}

#endif //__INK_INET_H__
